package com.lanyuan.thrid.wechat.utils;

import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

public class SignatureUtil {

	/**
	 * 生成 package 字符串
	 * 
	 * @param map
	 * @param paternerKey
	 * @return
	 */
	public static String generatePackage(Map<String, String> map, String paternerKey) {
		String sign = generateSign(map, paternerKey);
		Map<String, String> tmap = MapUtil.order(map);
		String s2 = MapUtil.mapJoin(tmap, false, true);
		return s2 + "&sign=" + sign;
	}

	/**
	 * 生成sign MD5 加密 toUpperCase
	 * 
	 * @param map
	 * @param paternerKey
	 * @return
	 */
	public static String generateSign(Map<String, String> map, String paternerKey) {
		if (map.containsKey("sign")) {
			map.remove("sign");
		}
		String str = MapUtil.mapJoin(map, false, false);
		return DigestUtils.md5Hex(str + "&key=" + paternerKey).toUpperCase();
	}

	/**
	 * 生成 paySign
	 * 
	 * @param map
	 * @param paternerKey
	 * @return
	 */
	public static String generatePaySign(Map<String, String> map, String paySignKey) {
		if (paySignKey != null) {
			map.put("appkey", paySignKey);
		}
		Map<String, String> tmap = MapUtil.order(map);
		String str = MapUtil.mapJoin(tmap, true, false);
		return DigestUtils.shaHex(str);
	}

}
